.PHONY: help test test-unit test-integration test-e2e test-performance test-coverage test-all clean build lint

# 项目信息
PROJECT_NAME := rdiot-demo
VERSION := $(shell git describe --tags --always --dirty)
BUILD_TIME := $(shell date +%Y-%m-%d_%H:%M:%S)

# 编译信息
LDFLAGS := -X main.Version=$(VERSION) -X main.BuildTime=$(BUILD_TIME)

# 测试配置
TEST_TIMEOUT := 300s
COVERAGE_DIR := coverage
COVERAGE_FILE := $(COVERAGE_DIR)/coverage.out
HTML_COVERAGE := $(COVERAGE_DIR)/coverage.html

help: ## 显示帮助信息
	@echo "RDIOT Demo - 消防物联网平台测试工具"
	@echo ""
	@echo "使用方法:"
	@echo "  make <target>"
	@echo ""
	@echo "可用目标:"
	@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "  %-20s %s\n", $$1, $$2}' $(MAKEFILE_LIST)

# ============================================================================
# 构建相关
# ============================================================================

build: ## 构建应用程序
	@echo "🔨 构建应用程序..."
	go build -ldflags "$(LDFLAGS)" -o bin/$(PROJECT_NAME) ./cmd/server

build-tools: ## 构建工具
	@echo "🔧 构建工具..."
	go build -o bin/migrate ./cmd/tools/migrate

clean: ## 清理构建文件
	@echo "🧹 清理构建文件..."
	rm -rf bin/
	rm -rf $(COVERAGE_DIR)/
	rm -rf logs/test_*.log

# ============================================================================
# 代码质量检查
# ============================================================================

lint: ## 运行代码检查
	@echo "🔍 运行代码检查..."
	@if command -v golangci-lint >/dev/null 2>&1; then \
		golangci-lint run; \
	else \
		echo "⚠️  golangci-lint 未安装，跳过代码检查"; \
		echo "💡 安装方式: go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest"; \
	fi

fmt: ## 格式化代码
	@echo "📝 格式化代码..."
	go fmt ./...
	go mod tidy

vet: ## 运行go vet
	@echo "🔍 运行go vet..."
	go vet ./...

# ============================================================================
# 测试相关
# ============================================================================

test-deps: ## 安装测试依赖
	@echo "📦 安装测试依赖..."
	go mod download
	go install github.com/stretchr/testify@latest

test-setup: ## 设置测试环境
	@echo "⚙️  设置测试环境..."
	@mkdir -p $(COVERAGE_DIR)
	@mkdir -p logs

test-unit: test-setup ## 运行单元测试
	@echo "🧪 运行单元测试..."
	go test -timeout $(TEST_TIMEOUT) -race -coverprofile=$(COVERAGE_FILE) \
		./tests/unit/... \
		./internal/eav/domain/... \
		./internal/eav/eav_service/... \
		./internal/eav/repository/... \
		-v

test-integration: test-setup ## 运行集成测试
	@echo "🔗 运行集成测试..."
	@if [ -z "$(SKIP_INTEGRATION_TESTS)" ]; then \
		go test -timeout $(TEST_TIMEOUT) -tags=integration \
			./tests/integration/... \
			-v; \
	else \
		echo "⏭️  跳过集成测试 (SKIP_INTEGRATION_TESTS=1)"; \
	fi

test-e2e: test-setup ## 运行端到端测试
	@echo "🌐 运行E2E测试..."
	@if [ -z "$(SKIP_E2E_TESTS)" ]; then \
		go test -timeout $(TEST_TIMEOUT) -tags=e2e \
			./tests/e2e/... \
			-v; \
	else \
		echo "⏭️  跳过E2E测试 (SKIP_E2E_TESTS=1)"; \
	fi

test-performance: test-setup ## 运行性能测试
	@echo "⚡ 运行性能测试..."
	@if [ -z "$(SKIP_PERFORMANCE_TESTS)" ]; then \
		go test -timeout $(TEST_TIMEOUT) -tags=performance \
			./tests/e2e/performance/... \
			-v -bench=. -benchmem; \
	else \
		echo "⏭️  跳过性能测试 (SKIP_PERFORMANCE_TESTS=1)"; \
	fi

test-coverage: test-unit ## 生成测试覆盖率报告
	@echo "📊 生成测试覆盖率报告..."
	@if [ -f $(COVERAGE_FILE) ]; then \
		go tool cover -html=$(COVERAGE_FILE) -o $(HTML_COVERAGE); \
		go tool cover -func=$(COVERAGE_FILE) | tail -1; \
		echo "📋 覆盖率报告已生成: $(HTML_COVERAGE)"; \
	else \
		echo "❌ 未找到覆盖率文件，请先运行单元测试"; \
	fi

test-all: test-unit test-integration test-e2e ## 运行所有测试
	@echo "✅ 所有测试完成"

test: test-unit ## 运行单元测试（默认）

# ============================================================================
# 快速测试命令
# ============================================================================

test-quick: ## 快速测试（只运行单元测试，无覆盖率）
	@echo "⚡ 快速测试..."
	go test -timeout 60s ./tests/unit/... -v

test-watch: ## 监控文件变化并自动运行测试
	@echo "👀 监控模式（需要安装 entr）..."
	@if command -v entr >/dev/null 2>&1; then \
		find . -name "*.go" | entr -c make test-quick; \
	else \
		echo "❌ 需要安装 entr: brew install entr (macOS) 或 apt install entr (Ubuntu)"; \
	fi

# ============================================================================
# 测试数据库管理
# ============================================================================

db-test-up: ## 启动测试数据库
	@echo "🗄️  启动测试数据库..."
	@if command -v docker >/dev/null 2>&1; then \
		docker run --name rdiot-test-db -d \
			-e POSTGRES_DB=rdiot_demo_test \
			-e POSTGRES_USER=postgres \
			-e POSTGRES_PASSWORD=Abc@123456 \
			-p 5433:5432 \
			postgres:15-alpine; \
		echo "✅ 测试数据库已启动在端口 5433"; \
	else \
		echo "❌ Docker 未安装，无法启动测试数据库"; \
	fi

db-test-down: ## 停止测试数据库
	@echo "🗄️  停止测试数据库..."
	@if command -v docker >/dev/null 2>&1; then \
		docker stop rdiot-test-db || true; \
		docker rm rdiot-test-db || true; \
		echo "✅ 测试数据库已停止"; \
	else \
		echo "❌ Docker 未安装"; \
	fi

# ============================================================================
# 性能测试
# ============================================================================

benchmark: ## 运行基准测试
	@echo "🏃 运行基准测试..."
	go test -bench=. -benchmem ./tests/unit/... ./internal/...

benchmark-eav: ## 运行EAV模块基准测试
	@echo "🏃 运行EAV基准测试..."
	go test -bench=. -benchmem ./tests/unit/eav_service/... ./internal/eav/...

# ============================================================================
# 报告生成
# ============================================================================

test-report: test-all test-coverage ## 生成完整测试报告
	@echo "📋 生成测试报告..."
	@echo "==================== 测试报告 ====================" > test-report.txt
	@echo "生成时间: $(BUILD_TIME)" >> test-report.txt
	@echo "项目版本: $(VERSION)" >> test-report.txt
	@echo "=================================================" >> test-report.txt
	@echo "" >> test-report.txt
	@if [ -f $(COVERAGE_FILE) ]; then \
		echo "📊 覆盖率统计:" >> test-report.txt; \
		go tool cover -func=$(COVERAGE_FILE) >> test-report.txt; \
	fi
	@echo "" >> test-report.txt
	@echo "✅ 测试报告已生成: test-report.txt"

# ============================================================================
# 开发辅助
# ============================================================================

dev-setup: test-deps build-tools ## 设置开发环境
	@echo "🚀 设置开发环境..."
	@echo "✅ 开发环境设置完成"

dev-test: fmt vet test-quick ## 开发者快速测试流程
	@echo "✅ 开发者测试完成"

ci: lint test-all ## CI流水线
	@echo "🚀 CI流水线完成"

# ============================================================================
# 环境变量说明
# ============================================================================

env-help: ## 显示环境变量说明
	@echo "🌍 环境变量说明:"
	@echo ""
	@echo "测试行为控制:"
	@echo "  SKIP_INTEGRATION_TESTS=1    跳过集成测试"
	@echo "  SKIP_E2E_TESTS=1           跳过E2E测试"
	@echo "  SKIP_PERFORMANCE_TESTS=1   跳过性能测试"
	@echo "  VERBOSE_OUTPUT=1           详细输出"
	@echo ""
	@echo "数据库配置:"
	@echo "  TEST_DB_HOST=localhost     测试数据库主机"
	@echo "  TEST_DB_PORT=5432          测试数据库端口"
	@echo "  TEST_DB_USER=postgres      测试数据库用户"
	@echo "  TEST_DB_PASSWORD=***       测试数据库密码"
	@echo ""
	@echo "性能测试配置:"
	@echo "  CONCURRENT_USERS=100       并发用户数"
	@echo "  PERF_TEST_DURATION=60      性能测试持续时间(秒)"
	@echo "  DATASET_SIZE=1000          测试数据集大小" 